/**
	@file GameServerCommand.cpp

	(c)2006 Palestar Inc
	@author Richard Lyle @date 2/23/2007 11:05:52 PM
*/

#include "GameServer.h"
#include "Standard/CommandLine.h"

//---------------------------------------------------------------------------------------------------

enum Command
{
	CMD_INVALID,	// invalid command
	CMD_CLEAN,		// wipe the worldContext clean
	CMD_LOOT,		// spawn a random loot at my position
};

struct CommandLookup
{
	const char *	pPattern;
	Command			nCommand;
	dword			flags;
	const char *	pHelp;
	dword			color;
};

static CommandLookup COMMAND_LOOKUP[] = 
{
	{ "/clean",		CMD_CLEAN,			MetaClient::SERVER,			"Clean all planets of structures/units",	GREY.RGB() },
	{ "/loot",		CMD_LOOT,			MetaClient::AWAY,	"Spawn random loot on my position"		,	GREY.RGB() },
	{ NULL,			CMD_INVALID,		0 }			// terminate the list, leave at end
};

//---------------------------------------------------------------------------------------------------

bool GameServer::processCommand( ClientContext & context, const char * pChat )
{
	dword nClientId = context.nClientId;
	dword nUserId = context.nUserId;

	// copy into a buffer so we can tokenize it
	CommandLine arguments( pChat, true );
	CharString pattern( arguments.argument( 0 ) );

	Command nCommand = CMD_INVALID;
	Color color( WHITE );

	// search the commands
	int c = 0;
	int n = pattern.length();			
	while( COMMAND_LOOKUP[c].pPattern != NULL )
	{
		// allow /aw to match against /away command by using strnicmp
		if ( strnicmp<char>( pattern, COMMAND_LOOKUP[c].pPattern, n ) == 0 )
		{
			// found a matching string, check the access rights
			if ( COMMAND_LOOKUP[c].flags != 1 && (context.MetaProfile.flags & COMMAND_LOOKUP[c].flags) == 1 )
			{
				sendChat( nClientId, "<color;808080>You do not have the rights to use this command!" );
				return false;
			}

			nCommand = COMMAND_LOOKUP[c].nCommand;
			color = COMMAND_LOOKUP[c].color;
			break;
		}

		// next command
		c++;
	}
	NounShip *pShip =NULL;
	switch( nCommand )
	{
	case CMD_LOOT:
		pShip = WidgetCast<NounShip>(context.pFocus);
		if(pShip)
		{
			if ( arguments.argumentCount() > 1 )
			{
				CharString sArg = arguments.restring( 1 );
				pShip->handleEnhancementDrop(sArg,NounEnhancement::classKey());
			}
			return true;
		}
		break;
	case CMD_CLEAN:
		{
			if ( arguments.argumentCount() > 1 )
			{
				CharString sConfirm = arguments.argument( 1 );
				if ( sConfirm.compareNoCase( "planets" ) == 0 )
				{
					sendUserChat( nUserId, "Cleaning planets..." );

					// reset the entire map, remove all structures from all planets, set nTeamId to 0, reset all planet resources and population
					for(int i=0;i<worldContext()->zoneCount();i++)
					{
						NodeZone * pZone = worldContext()->zone( i );
						for(int j=0;j<pZone->childCount();j++)
						{
							if ( WidgetCast<NounPlanet>( pZone->child(j) ) )
							{
								NounPlanet * pPlanet = (NounPlanet *)pZone->child(j);
								
								pPlanet->setTeamId( 0 );
								pPlanet->setPopulation( 0 );
								pPlanet->setResources( 1000 );
								pPlanet->setup();
								pPlanet->detachAllNodes();

								pPlanet->resync();
							}
						}
					}
				}
				else if ( sConfirm.compareNoCase( "ships" ) == 0 )
				{
					Queue< NounShip * > remove;

					// reset the entire map, remove all structures from all planets, set nTeamId to 0, reset all planet resources and population
					for(int i=0;i<worldContext()->zoneCount();i++)
					{
						NodeZone * pZone = worldContext()->zone( i );
						for(int j=0;j<pZone->childCount();j++)
						{
							NounShip * pSelf = WidgetCast<NounShip>( pZone->child(j) );
							if ( pSelf != NULL )
								remove.push( pSelf );
						}
					}

					sendUserChat( nUserId, CharString().format("Removing %d ships from the worldContext...", remove.size()) );
					while( remove.valid() )
					{
						(*remove)->detachSelf();
						remove.next();
					}

				}
			}
			else
				sendUserChat( nUserId, "<color;808080>Usage: /clean [planets|ships]" );
		}
		return true;
	}

	return WorldServer::processCommand( context, pChat );
}

//---------------------------------------------------------------------------------------------------
//EOF
